﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace RestaurantManagerWebApp.Pages.TableManagement
{
    public partial class TableAllocation : System.Web.UI.Page
    {

        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void buttonManageTables_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/TableManagement/TableManagement.aspx");
        }

        protected void buttonAllocateTables_Click(object sender, EventArgs e)
        {

        }

        protected void buttonReturn_Click(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkButtonl = (LinkButton)e.Row.FindControl("LinkButton1");
                linkButtonl.Attributes.Add("onclick", "javascript:return " +
                "confirm('Are you sure you want to delete Table No. " +
                DataBinder.Eval(e.Row.DataItem, "table_no") + "?')");
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string table_no = e.CommandArgument.ToString();
            if (e.CommandName == "Delete")
            {
                labelStatus.Visible = true;
                labelStatus.Text = DeleteRecordByID(table_no);
            }
            else if (e.CommandName == "Update")
            {
                labelStatus.Visible = true;
                labelStatus.Text = "Table " + table_no + " update successfully.";
            }
        }

        private string DeleteRecordByID(string table_no)
        {
            string r = "";

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandText = "Update Orders set table_no = 'SAMPLE' where table_no = @table_no;";
            cmd.Parameters.AddWithValue("@table_no", table_no);
            int affectedRows = cmd.ExecuteNonQuery();

            r = "Number of orders changed table_no is " + affectedRows;

            return r;
        }

        protected void ddlNewTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            int tablesAdding = Convert.ToInt32(ddlNewTables.SelectedValue);

            Response.Redirect("TableManagement.aspx?table=" + ddlNewTables.SelectedValue);
        }

    }
}